﻿using System;
using System.Windows;
using System.Windows.Data;
using System.Windows.Markup;
using PurchaseSales.Manage.Common.Extensions;

namespace PurchaseSales.Manage.Common.Markup
{
    public class VisualExtension:BaseMarkupExtension
    {
        public VisualExtension()
        { }

        [ConstructorArgument("Type")]
        public Type Type { get; set; }

        [ConstructorArgument("ElementName")]
        public string ElementName { get; set; }

        [ConstructorArgument("ChildName")]
        public string ChildName { get; set; }

        [ConstructorArgument("IsRoot")]
        public int VisualIndex { get; set; }

        public override object ProvideValue()
        {
            var binding = new Binding() { ElementName = ElementName };
            TargetObject.SetBinding(ElementProperty, binding);
            Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                {
                    var element = TargetObject.GetBindingExpression(ElementProperty).DataItem as FrameworkElement;
                    FrameworkElement obj;
                    if (VisualIndex > 0)
                        obj = element.FindVisualChild(VisualIndex);
                    else
                    {
                        if (string.IsNullOrEmpty(this.ChildName))
                            obj = element.FindVisualChild(Type);
                        else
                            obj = element.FindVisualChild(ChildName);
                    }
                    if (obj != null)
                        TargetObject.SetBinding(TargetProperty, new Binding() { Source = obj });
                }), System.Windows.Threading.DispatcherPriority.Render);
            return null;
        }

        public static object GetElement(DependencyObject obj)
        {
            return (object)obj.GetValue(ElementProperty);
        }

        public static void SetElement(DependencyObject obj, object value)
        {
            obj.SetValue(ElementProperty, value);
        }

        public static readonly DependencyProperty ElementProperty =
            DependencyProperty.RegisterAttached("Element", typeof(object), typeof(VisualExtension),
                                                new UIPropertyMetadata(null));
    }
}